package pshop.servlet.util;

import pshop.Warenkorb;
import pshop.servlet.ServletConstants;
import pshop.servlet.util.SessionUtils;
import com.google.common.base.Strings;

import javax.servlet.http.HttpServletRequest;

public abstract class WkUtils
{
   public static void updateWk(HttpServletRequest req, String action)
   {
      String artIdStr = req.getParameter(ServletConstants.PARAM_ARTICLE_ID);
      if (Strings.isNullOrEmpty(artIdStr))
         return;
      int artId = Integer.parseInt(artIdStr);

      Warenkorb wk = SessionUtils.getSessionWarenkorb(req);
      if (ServletConstants.PARAM_ACTION_ADD.equals(action))
      {
         wk.addArtikel(artId);
      }
      else if (ServletConstants.PARAM_ACTION_REMOVE.equals(action))
      {
         wk.remove(artId);
      }
      else if (ServletConstants.PARAM_ACTION_DELETE.equals(action))
      {
         wk.delete(artId);
      }
      req.getSession().setAttribute(ServletConstants.ATTR_WK, wk);
   }

}